/*
 * {comment of class}
 *
 * @author (C) <a href="http://www.carfield.com.hk">Carfield Yim</a>, 2004
 */
package hk.com.carfield.action.process;

import hk.com.carfield.core.Context;
import hk.com.carfield.util.StringUtil;
import hk.com.carfield.web.Request;

import java.io.File;

public final class Rename implements Process {
	private static final String filename = "filename";

	public static File rename(final File l, final String name) {
		if (StringUtil.notBlank(name) && !l.getName().equals(name)) {
			final File newFile = new File(l.getParentFile(), name);
			l.renameTo(newFile);
			return newFile;
		} else {
			return l;
		}
	}

	public String actionCode() {
		return "rename";
	}

	public boolean adminOnly() {
		return true;
	}

	public File process(final Context c, final Request req, final File l) {
		// TODO add validation
		return Rename.rename(l, req.getParameter(Rename.filename))
				.getParentFile();
	}
}